package com.primb.bench.project.home.model;

import com.primb.bench.common.constant.AppConstant;
import com.primb.bench.common.module.http.service.ApiClient;
import com.primb.bench.common.module.http.service.ApiService;
import com.primb.bench.project.home.contract.AddInfoContract;
import com.primb.bench.project.home.entity.HttpLoanTreeEntity;
import com.primb.bench.project.home.entity.HttpInfoEntity;
import com.primb.bench.project.home.entity.HttpStrEntity;
import com.primb.bench.project.home.entity.KeyEntity;
import com.primb.netlibs.converter.RxSchedulerUtils;
import com.primb.netlibs.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.Observable;
import rx.functions.Func1;

import static android.media.CamcorderProfile.get;
import static com.primb.bench.common.module.http.service.ApiClient.initService;

/**
 * Created by Administrator on 2017/05/04
 */

public class AddInfoModelImpl implements AddInfoContract.Model {

    public static final String KEY_ENSURE_MAIN = "L07";//主担保方式
    public static final String KEY_ENSURE_CONCRETENESS = "L14";//主担保方式
    public static final String KEY_CUSTOMER_SIZE = "L04";//客户规模
    public static final String KEY_CUSTOMER_CREDIT = "L03";//客户信用
    public static final String KEY_CUSTOMER_TYPE = "L02";//客户类型
    public static final String KEY_LOAN_RATE = "L06";//利率类型
    public static final String KEY_LOAN_REPAYMENT = "L17";//还款方式


    @Override
    public Observable<List<KeyEntity>> getPickerList(int code, String type) {
        List<KeyEntity> dataList = new ArrayList<>();
        String codeTypeInfo = "";
        if (AppConstant.ADD_LOAN_DATE == code) {
            dataList.add(new KeyEntity("年", "1"));
            dataList.add(new KeyEntity("月", "0"));
            return Observable.just(dataList).compose(RxSchedulerUtils.normalSchedulersTransformer());
        } else {
            switch (code) {
                case AppConstant.ADD_CUSTOMER_TYPE:
                    codeTypeInfo = KEY_CUSTOMER_TYPE;
                    break;
                case AppConstant.ADD_CUSTOMER_CREDIT:
                    codeTypeInfo = KEY_CUSTOMER_CREDIT;
                    break;
                case AppConstant.ADD_CUSTOMER_SIZE:
                    codeTypeInfo = KEY_CUSTOMER_SIZE;
                    break;
                case AppConstant.ADD_LOAN_TYPE:
                    return initService(ApiService.class).getLoanCurrencyList().flatMap(new Func1<HttpInfoEntity, Observable<List<KeyEntity>>>() {
                        @Override
                        public Observable<List<KeyEntity>> call(HttpInfoEntity httpCurreyEntity) {
                            try {
                                convertData(dataList, httpCurreyEntity);
                            } catch (Exception e) {
                                Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                            }
                            return Observable.just(dataList);
                        }
                    }).compose(RxSchedulerUtils.normalSchedulersTransformer());
                case AppConstant.ADD_LOAN_BUSINESS_TYPE:
                    return initService(ApiService.class).getLoanBusinessList(type).flatMap(new Func1<HttpLoanTreeEntity, Observable<List<KeyEntity>>>() {
                        @Override
                        public Observable<List<KeyEntity>> call(HttpLoanTreeEntity httpEntity) {
                            if (!httpEntity.isSuccess())
                                Observable.error(new ApiException(httpEntity.getMsg()));
                            try {
                                convertList(dataList, httpEntity.getData());
                            } catch (Exception e) {
                                Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                            }
                            return Observable.just(dataList);
                        }
                    }).compose(RxSchedulerUtils.normalSchedulersTransformer());
                case AppConstant.ADD_LOAN_INDUSTRY:
                    return initService(ApiService.class).getLoanIndustryList().flatMap(new Func1<HttpInfoEntity, Observable<List<KeyEntity>>>() {
                        @Override
                        public Observable<List<KeyEntity>> call(HttpInfoEntity httpCurreyEntity) {
                            try {
                                convertData(dataList, httpCurreyEntity);
                            } catch (Exception e) {
                                Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                            }
                            return Observable.just(dataList);
                        }
                    }).compose(RxSchedulerUtils.normalSchedulersTransformer());
                case AppConstant.ADD_LOAN_REPAYMENT_TYPE:
                    codeTypeInfo = KEY_LOAN_REPAYMENT;
                    break;
                case AppConstant.ADD_LOAN_RATE_TYPE:
                    codeTypeInfo = KEY_LOAN_RATE;
                    break;
                case AppConstant.ADD_ENSURE_MAIN_TYPE:
                    codeTypeInfo = KEY_ENSURE_MAIN;
                    break;
                case AppConstant.ADD_ENSURE_CONCRETENESS:
//                    return initService(ApiService.class).getLoanConcretenessList(type).flatMap(new Func1<List<HttpLoanTreeEntity>, Observable<List<KeyEntity>>>() {
//                        @Override
//                        public Observable<List<KeyEntity>> call(List<HttpLoanTreeEntity> entity) {
//                            try {
//                                convertList(dataList, entity);
//                            } catch (Exception e) {
//                                Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
//                            }
//                            return Observable.just(dataList);
//                        }
//                    }).compose(RxSchedulerUtils.normalSchedulersTransformer());
                    codeTypeInfo = KEY_ENSURE_CONCRETENESS;
                    break;
//                case AppConstant.ADD_ENSURE_OBJECT_TYPE:
//                    dataList.add(new KeyEntity("质押品类型", "质押品类型"));
//                    dataList.add(new KeyEntity("质押品类型", "质押品类型"));
//                    return Observable.just(dataList).compose(RxSchedulerUtils.normalSchedulersTransformer());
            }

            return ApiClient.initService(ApiService.class).getLoanInfoList(codeTypeInfo).flatMap(new Func1<HttpInfoEntity, Observable<List<KeyEntity>>>() {
                @Override
                public Observable<List<KeyEntity>> call(HttpInfoEntity httpCurreyEntity) {
                    try {
                        convertData(dataList, httpCurreyEntity);
                    } catch (Exception e) {
                        Observable.error(new ApiException("服务器数据异常，请联系服务器人员解决"));
                    }
                    return Observable.just(dataList);
                }
            }).compose(RxSchedulerUtils.normalSchedulersTransformer());
        }
    }

    @Override
    public Observable<HttpStrEntity> deleteAllEnsureObject(String priceId) {
        return ApiClient.initService(ApiService.class).deleteAllEnsureObject(priceId).compose(RxSchedulerUtils.normalSchedulersTransformer());
    }

    public static void convertList(List<KeyEntity> dataList, List<HttpLoanTreeEntity.DataBean> entity) {
        for (int i = 0; i < entity.size(); i++) {
            if (entity.get(i).getLeafFlag() != null && entity.get(i).getLeafFlag().equals("1")) {
                dataList.add(new KeyEntity(entity.get(i).getText(), entity.get(i).getId()));
            } else {
                convertList(dataList, entity.get(i).getChildren());
            }
        }
    }

    public static void convertData(List<KeyEntity> dataList, HttpInfoEntity httpCurreyEntity) {
        for (int i = 0; i < httpCurreyEntity.getData().size(); i++) {
            KeyEntity keyEntity = new KeyEntity();
            keyEntity.setKey(httpCurreyEntity.getData().get(i).getDesc());
            keyEntity.setValue(httpCurreyEntity.getData().get(i).getCode());
            dataList.add(keyEntity);
        }
    }
}